const express = require("express");
const router = express.Router();
const conn = require("../util/sql.js");
const multer = require("multer");

// 配置上传文件模块
let storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'uploads')
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname)
    }
})

let upload = multer({
    storage: storage
})

// 调用应用中间件
router.use(express.urlencoded());


// 获取英雄数据
router.get("/getHeroList", (req, res) => {
    let {
        heroName
    } = req.query;

    let sql = `select id,name,gender,img from heros where isdelete=0`;
    if (heroName) sql += ` and name like "%${heroName}%"`;

    conn.query(sql, (err, result) => {
        if (err) {
            console.log(err);
            res.status(500).json({
                code: 500,
                msg: "获取英雄失败"
            });
            return;
        }
        res.json({
            code: 200,
            msg: "获取英雄成功",
            heros: result
        });
    })
})

// 更新英雄数据
router.post("/updateHero", (req, res) => {
    let {
        id,
        name,
        gender,
        img
    } = req.body;
    console.log(req.body);

    let infoArr = [];

    if (name) {
        infoArr.push(`name="${name}"`);
    }
    if (gender) {
        infoArr.push(`gender="${gender}"`);
    }
    if (img) {
        infoArr.push(`img="${img}"`);
    }

    infoArr = infoArr.join(",");
    console.log(infoArr);

    let sql = `update heros set ${infoArr} where id=${id}`;
    console.log(sql);

    conn.query(sql, (err, result) => {
        if (err) {
            console.log(err);
            res.status(500).json({
                code: 500,
                msg: "更新英雄失败"
            });
            return;
        }

        if (result.affectedRows > 0) {
            res.json({
                code: 200,
                msg: "更新英雄成功",
                heros: result
            });
        }
    })


})

// 添加英雄数据
router.post("/addHero", (req, res) => {
    let {
        name,
        gender,
        img
    } = req.body;

    let infoArr = [];
    let keyArr = [];

    if (name) {
        keyArr.push("name");
        infoArr.push(`"${name}"`);
    }
    if (gender) {
        keyArr.push("gender");
        infoArr.push(`"${gender}"`);
    }
    if (img) {
        keyArr.push("img");
        infoArr.push(`"${img}"`);
    }

    infoArr = infoArr.join(",");

    let sql = `insert into heros(${keyArr}) values (${infoArr})`;

    conn.query(sql, (err, result) => {
        if (err) {
            console.log(err);
            res.status(500).json({
                code: 500,
                msg: "添加英雄失败"
            });
            return;
        }

        if (result.affectedRows > 0) {
            res.json({
                code: 200,
                msg: "添加英雄成功",
                heros: result
            });
        }
    })


})

// 删除英雄数据
router.get("/delHeroById", (req, res) => {
    let {
        id
    } = req.query;

    let sql = `update heros set isdelete=1 where id=${id}`;

    conn.query(sql, (err, result) => {
        if (err) {
            console.log(err);
            res.status(500).json({
                code: 500,
                msg: "删除英雄失败"
            });
            return;
        }

        if (result.affectedRows > 0) {
            res.json({
                code: 200,
                msg: "删除英雄成功",
                heros: result
            });
        }
    })
})

// 上传头像
router.post("/uploadFile", upload.single('file_data'), (req, res) => {
    let {
        file
    } = req;
    console.log(file);
    // let {
    //     id
    // } = req.body;
    let imgSrc = "http://127.0.0.1:3000/uploads/" + file.originalname;
    // let sql = `update heros set img="${imgSrc}" where id=${id}`;
    // conn.query(sql, (err, result) => {
    // if (err) {
    //     res.status(500).json({
    //         code: 500,
    //         msg: "上传文件失败"
    //     });
    // }
    res.status(200).json({
        code: 200,
        msg: "上传文件成功",
        src: imgSrc
    });
    // })
})

module.exports = router;